package com.link_with_health.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.HashMap;

@Component
public class RegexUtil {
    @Value("${config.valueRegex.phone}")
    private String phoneRegex;

    @Value("${config.valueRegex.Email}")
    private String emailRegex;

    private HashMap<String,Pattern> patternMap;

    @PostConstruct
    private void initPattern(){
        patternMap = new HashMap<>();
        patternMap.put("phone",Pattern.compile(phoneRegex));
        patternMap.put("email",Pattern.compile(emailRegex));
    }

    /**
     * 测试是否为手机号码
     * @param phone 手机号码
     * @return 判断结果
     */
    public boolean isPhone(String phone){
        if(phone == null)
            return false;
        Matcher matcher = patternMap.get("phone").matcher(phone);
        return matcher.matches();
    }

    /**
     * 测试是否为邮箱地址
     * @param email 邮件地址
     * @return 判断结果
     */
    public boolean isEmail(String email){
        if(email == null)
            return false;
        Matcher matcher = patternMap.get("email").matcher(email);
        return matcher.matches();
    }

    public boolean isEmpty(String str){
        return str == null || str.isEmpty();
    }

}
